#include "homepagewidget.h"
#include "ui_homepagewidget.h"
#include <QVBoxLayout>
#include "util.h"
#include <videobox.h>
#include "./model/datacenter.h"
HomePageWidget::HomePageWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HomePageWidget)
{
    ui->setupUi(this);

    initKindAndTags();

    initRefreshAndTopBtn();

    initVideos();
}

HomePageWidget::~HomePageWidget()
{
    delete ui;
}

void HomePageWidget::initKindAndTags()
{
    // 分类
    auto dataCenter = model::DataCenter::getInstance();
    auto KindAndTagPtr = dataCenter->getKindAndTagClassPtr();
    auto kinds = KindAndTagPtr->getAllKinds();
    // 创建分类按钮
    QPushButton* kindBtn = buildSelectBtn(ui->classifys, "#ECEFFF", "分类");
    ui->classifyHLayout->addWidget(kindBtn);

    // 具体的分类按钮
    for(auto& kind : kinds)
    {
        QPushButton* kindItemBtn = buildSelectBtn(ui->classifys, "#222222", kind);
        ui->classifyHLayout->addWidget(kindItemBtn);

        // 给分类按钮绑定槽函数
        connect(kindItemBtn, &QPushButton::clicked, this, [=]{
            onKindBtnClicked(kindItemBtn);
        });
    }

    ui->classifyHLayout->setSpacing(8);
    // 分类和该分类下所有标签映射
    auto tags = KindAndTagPtr->getTagsByKind(kinds[0]).keys();

    // 创建分类下的所有标签，默认情况下创建的0个分类下的标签
    resetTags(tags);
}

QPushButton* HomePageWidget::buildSelectBtn(QWidget *parent, const QString &color, const QString &text)
{
    QPushButton* btn = new QPushButton(text, parent);
    btn->setStyleSheet("color : " + color);
    btn->setMinimumHeight(26);
    // 18:文本到按钮边界的距离
    btn->setFixedWidth(text.size()*16 + 18 + 18);
    return btn;
}

void HomePageWidget::resetTags(const QList<QString> &tags)
{
    // 创建标签的声明
    QPushButton* tag = buildSelectBtn(ui->labels, "#ECEFFF", "标签");
    ui->labelHLayout->addWidget(tag);

    // 创建具体每个标签
    for(auto& tagText : tags)
    {
        QPushButton* tagItem = buildSelectBtn(ui->labels, "#666666", tagText);
        ui->labelHLayout->addWidget(tagItem);

        connect(tagItem, &QPushButton::clicked, this, [=]{
            onTagBtnClicked(tagItem);
        });
    }

    ui->labelHLayout->setSpacing(4);
}

void HomePageWidget::initRefreshAndTopBtn()
{
    // 先创建一个Widget
    QWidget* refreshTopWidget = new QWidget(this);
    refreshTopWidget->setFixedSize(42, 94);
    refreshTopWidget->setStyleSheet("QPushButton{"
                                    "border-radius : 21px;"
                                    "border : none;"
                                    "background-color : #DDDDDD;}"
                                    "QPushButton:hover{background-color : #666666;}");
    QVBoxLayout* layout = new QVBoxLayout(refreshTopWidget);

    // 创建按钮
    QPushButton* topBtn = new QPushButton(refreshTopWidget);
    topBtn->setFixedSize(42, 42);
    topBtn->setStyleSheet("border-image : url(:/images/homePage/zhiding.png)");
    layout->addWidget(topBtn);

    QPushButton* refreshBtn = new QPushButton(refreshTopWidget);
    refreshBtn->setFixedSize(42, 42);
    refreshBtn->setStyleSheet("border-image : url(:/images/homePage/shuaxin.png)");
    layout->addWidget(refreshBtn);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(10);

    refreshTopWidget->move(1278, 618);

    connect(topBtn, &QPushButton::clicked, this, &HomePageWidget::onTopBtnClicked);
    connect(refreshBtn, &QPushButton::clicked, this, &HomePageWidget::onRefreshBtnClicked);
}

void HomePageWidget::initVideos()
{
    for(int i = 0; i < 16; i++)
    {
        VideoBox* videoBox = new VideoBox();

        // 一行显示4个视频
        ui->videoGLayout->addWidget(videoBox, i / 4, i % 4);
    }
}

void HomePageWidget::onKindBtnClicked(QPushButton *clickedKindBtn)
{
    clickedKindBtn->setStyleSheet("background-color : #F1FDFF;"
                               "color : #3ECEFF;");
    // 清除之前点击过的分类按钮上的高亮显示
    // 获取所有分类按钮--即获取classify中所有类型为QPushButton*孩子，因为所有分类按钮的父元素都是ui->classifys
    QList<QPushButton*> kindBtns = ui->classifys->findChildren<QPushButton*>();
    for(auto kindBtn : kindBtns)
    {
        if(kindBtn != clickedKindBtn)
        {
            kindBtn->setStyleSheet("color : #222222;");
        }
    }

    // 重新添加当前点击分类按钮对应的所有标签
    QList<QPushButton*> tagBtns = ui->labels->findChildren<QPushButton*>();
    for(auto tagBtn : tagBtns)
    {
        ui->labelHLayout->removeWidget(tagBtn);
        delete tagBtn;
    }
    auto dataCenter = model::DataCenter::getInstance();
    auto kindAndTagPtr = dataCenter->getKindAndTagClassPtr();
    resetTags(kindAndTagPtr->getTagsByKind(clickedKindBtn->text()).keys());
}

void HomePageWidget::onTagBtnClicked(QPushButton *clickedTagBtn)
{
    clickedTagBtn->setStyleSheet("background-color : #F1FDFF;"
                           "color : #3ECEFF;");

    QList<QPushButton*> tagBtns = ui->labels->findChildren<QPushButton*>();
    for(auto tagBtn : tagBtns)
    {
        if(tagBtn != clickedTagBtn)
        {
            tagBtn->setStyleSheet("color : #666666;");
        }
    }
}

void HomePageWidget::onTopBtnClicked()
{
    LOG() << "置顶按钮点击";
}

void HomePageWidget::onRefreshBtnClicked()
{
    LOG() << "刷新按钮点击";
}

















